package com.nuc.workflowserver.controller;

import com.nuc.workflowserver.common.model.ResultModel;
import com.nuc.workflowserver.mapper.IUserRoleMapper;
import com.nuc.workflowserver.model.dto.ConditionDTO;
import com.nuc.workflowserver.model.dto.UserInfoDTO;
import com.nuc.workflowserver.model.pojo.User;
import com.nuc.workflowserver.model.pojo.UserInfo;
import com.nuc.workflowserver.model.pojo.UserRole;
import com.nuc.workflowserver.model.vo.UserInfoVO;
import com.nuc.workflowserver.service.IUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("userinfo")
@Api(value = "用户信息接口", description = "提供用户信息维护")
public class UserInfoController {

    @Autowired
    IUserInfoService iUserInfoService;

    @Autowired
    IUserRoleMapper iUserRoleMapper;

    @RequestMapping("/selectByPrimary")
    @ApiOperation("根据主键查询用户信息")
    public ResultModel selectByPrimary(@RequestBody UserInfo userInfo,
            HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (null != user && null != user.getId()) {
            UserInfo userInfoRes = iUserInfoService
                    .selectByPrimary(new UserInfo(user.getId()));
            return new ResultModel(userInfoRes, true);
        }
        return new ResultModel("系统异常请联系管理员", false);
    }

    @PostMapping("/update")
    @ApiOperation("根据主键更新用户信息")
    public ResultModel update(@RequestBody UserInfo userInfo) {
        if (null != userInfo && null != userInfo.getId()) {
            Integer res = iUserInfoService.update(userInfo);
            if (res > 0) {
                return new ResultModel("更新成功！", true);
            }
        }
        return new ResultModel("更新失败！请联系管理员", false);
    }

    @PostMapping("/insertOrUpadte")
    @ApiOperation("新增或更新用户信息")
    public ResultModel insertOrupadte(@RequestBody UserInfoDTO userInfoDTO) {
        return iUserInfoService.insertOrUpadte(userInfoDTO);
    }

    @PostMapping("/userinfoPage")
    @ApiOperation("获取员工信息列表")
    public ResultModel userinfoPage(@RequestBody ConditionDTO conditionDTO) {
        UserInfo userInfo = new UserInfo();
        userInfo.setFlowId(conditionDTO.getFlowId());
        userInfo.setUsername(conditionDTO.getUsername());
        return new ResultModel(iUserInfoService
                .selectPage(userInfo, conditionDTO.getPageNum(),
                        conditionDTO.getPageSize()), true);
    }

    @PostMapping("/selectUserInfoVOByPrimary")
    @ApiOperation("根据主键获取员工信息VO")
    public ResultModel selectUserInfoVOByPrimary(
            @RequestBody UserInfo userInfo) {
        if (null == userInfo || null == userInfo.getId()) {
            return new ResultModel("请求参数有误，请联系管理员！", false);
        }
        UserInfoVO userInfoVO = iUserInfoService
                .selectUserInfoVOByPrimary(userInfo.getId());
        return new ResultModel(userInfoVO, true);
    }

    @PostMapping("/deleteUserInfo")
    @ApiOperation("逻辑删除员工")
    public ResultModel deleteUserInfo(@RequestBody UserInfo userInfo) {
        Integer res = iUserInfoService.delete(userInfo);
        if (res > 0) {
            return new ResultModel("删除成功！", true);
        } else {
            return new ResultModel("删除失败！", true);
        }
    }

    @GetMapping("/checkRole")
    @ApiOperation("用户角色校验")
    public ResultModel checkRole(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (null != user && null != user.getId()) {
            UserRole userRole = iUserRoleMapper
                    .selectByPrimary(new UserRole(user.getId()));
            if ("1".equals(userRole.getRoleId())) {
                return new ResultModel("您角色为管理员！", true);
            } else if ("2".equals(userRole.getRoleId())) {
                return new ResultModel("您角色为普通员工，无权限操作！", false);
            }
        }
        return null;
    }

}
